package com.yangcangclub.banner.domain;

import com.alibaba.druid.util.StringUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.yangcangclub.banner.base.BannerBaseDomain;
import com.yangcangclub.banner.bean.BannerInfo;
import com.yangcangclub.commons.exception.ApolloException;
import com.yangcangclub.commons.result.MessageEnum;
import com.yangcangclub.commons.utils.DateUtil;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/2/23 0023.
 */
@Component
public class BannerProfile extends BannerBaseDomain {


    /**
     * 添加轮播
     * @return
     */
    public boolean insertBanner(String name,String url,String image,
                                String startDate,String endDate,int createUserId) {

        BannerInfo bannerInfo = new BannerInfo();
        bannerInfo.setName(name);
        bannerInfo.setUrl(url);
        bannerInfo.setImage(image);
        bannerInfo.setStartDate((DateUtil.StringConvertToDate(startDate)));
        bannerInfo.setEndDate((DateUtil.StringConvertToDate(endDate)));
        bannerInfo.setCreateUserId(createUserId);
        super.getMasterJdbc().insert("banner.BannerMapper.insertBanner", bannerInfo);

        int id = bannerInfo.getId();

        if(id == 0) {
            throw new ApolloException(MessageEnum.DATABASE_ERROR);
        }else {
            // 只更新排序字段
            bannerInfo.setOrderSort(id);
            this.updateBannerOrderSort(bannerInfo);
        }
        return true;
    }

    /**
     * 更新
     * @param id
     * @param name
     * @param image
     * @param url
     * @param startDate
     * @param endDate
     * @return
     */
    public boolean updateBanner(int id, String name, String image, String url,
                                String startDate,String endDate){
        boolean flag = false;
        BannerInfo b = new BannerInfo();
        b.setId(id);
        b.setName(name);
        b.setImage(image);
        b.setUrl(url);
        if(!StringUtils.isEmpty(startDate)){
            b.setStartDate((DateUtil.StringConvertToDate(startDate)));
        }
        if(!StringUtils.isEmpty(endDate)){
            b.setEndDate((DateUtil.StringConvertToDate(endDate)));
        }


        int num =   super.getMasterJdbc().update("banner.BannerMapper.updateBannerById", b);
        if(num>0){
            flag =true;
        }

        return flag;
    }

    /**
     * 删除轮播图
     * @return
     */
    public boolean deleteBanner(int id) {
        BannerInfo bannerInfo = new BannerInfo();
        bannerInfo.setId(id);
        super.getMasterJdbc().update("banner.BannerMapper.deleteBannerById", bannerInfo);
        return true;
    }

    /**
     * 批量删除
     */
    public boolean  delBannerByIds(String ids){
        if(StringUtils.isEmpty(ids)){return false;}
        int j = 0;
        String [] id = ids.split(",");
        for(int i = 0 ; i <id.length; i ++){
            int tempId = Integer.parseInt(id[i]);
            if(tempId>0){
                this.deleteBanner(tempId);
                j++;
            }
        }
        if(j==0){
            return false;
        }else{
            return true;
        }
    }



    /**
     * 移动
     * @param type up
     * @param id
     * @return
     */
    public boolean moveBanner(int id ,String type,int orderSort){
        if(type==null){
            throw new ApolloException(MessageEnum.PARMA_FORMAT_ERROR);
        }else if(orderSort<0){
            throw new ApolloException(MessageEnum.PARMA_FORMAT_ERROR);
        }
        int temp_order_sort = 0;
        BannerInfo e = new BannerInfo();
        BannerInfo e1 = new BannerInfo();
        Map<String,Object> map= Maps.newHashMap();
        map.put("id",id);
        map.put("order_sort",orderSort);

        if("up".equalsIgnoreCase(type)){  // 上移
            e1 = super.getSlaveJdbc().selectOne("banner.BannerMapper.queryUpOrderSort", map);
        }else if("down".equalsIgnoreCase(type)){ // 下移
            e1 = super.getSlaveJdbc().selectOne("banner.BannerMapper.queryDownOrderSort", map);
        } else {
            throw new ApolloException(MessageEnum.PARMA_FORMAT_ERROR);
        }
        temp_order_sort = e1.getOrderSort();
        e.setOrderSort(temp_order_sort);
        e.setId(id);
        e1.setOrderSort(orderSort);
        boolean flag = this.updateBannerOrderSort(e);
        boolean flag1 = this.updateBannerOrderSort(e1);
        if(flag&&flag1){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 修改排序信息
     */
    public boolean  updateBannerOrderSort(BannerInfo b){
        if(b.getId()==0)return false;
        int i= super.getMasterJdbc().update("banner.BannerMapper.updateBannerOrderSort", b);
        if(i!=1){
            return false;
        }else{
            return true;
        }
    }


    /**
     * 发布&取消发布
     * @param status
     * @param id
     * @return
     */
    public boolean updatePublishStatus(int id,int status,int pubUserId,String pubUserName){
        Map<String,Object> map= Maps.newHashMap();
        map.put("id",id);
        map.put("status",status);
        map.put("pubUserId",pubUserId);
        map.put("pubUserName",pubUserName);
        if(id==0||status==0) return  false;
        int i= super.getMasterJdbc().update("banner.BannerMapper.updatePublishStatus", map);
        if(i!=1){
            return false;
        }else{
            return true;
        }
    }

    /**
     * 获取 List
     */
    public List<BannerInfo> getShowBannerList(int start, int pageSize){
        if(start<0||pageSize>50||pageSize<0){
            throw new ApolloException(MessageEnum.PARMA_RANGE_ERROR);
        }
        Map<String,Object> map= Maps.newHashMap();
        map.put("start",start);
        map.put("pageSize",pageSize);

        List<BannerInfo> list= super.getSlaveJdbc().selectList("banner.BannerMapper.selectShowBannerList", map);

        return list;
    }

    /**
     *  管理List
     */
    public List<BannerInfo> getManagerBannerList(int start, int pageSize,String search){
        if(start<0||pageSize>50||pageSize<0){
            throw new ApolloException(MessageEnum.PARMA_RANGE_ERROR);
        }
        Map<String,Object> map= Maps.newHashMap();
        map.put("start",start);
        map.put("pageSize", pageSize);
        map.put("search",search);
        List<BannerInfo> list = super.getSlaveJdbc().selectList("banner.BannerMapper.selectManagerBannerList", map);
        if(list.isEmpty()){
            list = Lists.newArrayList();
        }
        return list;
    }


    /**
     * 获取管理Count
     */
    public int getManagerBannerNum( int start, int pageSize,String search){
        if(start<0||pageSize>50||pageSize<0){
            throw new ApolloException(MessageEnum.PARMA_RANGE_ERROR);
        }
        Map<String,Object> map= Maps.newHashMap();
        map.put("start",start);
        map.put("pageSize", pageSize);
        map.put("search",search);
        int total= super.getSlaveJdbc().selectOne("banner.BannerMapper.selectManagerBannerCount", map);
        return total;
    }

}
